/**
 * 
 */
package com.ai.service.cms.info.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.ai.common.constant.WseSectionConstants;
import com.ai.common.constant.WseSectionConstantsHy;
import com.ai.common.exception.AlikAssert;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.common.utils.CommonUtil;
import com.ai.manager.edu.cms.info.InfoDetailManager;
import com.ai.manager.edu.cms.user.BackendOperatorManager;
import com.ai.model.edu.cms.backoperator.vo.BackendOperatorVO;
import com.ai.model.edu.cms.info.InfoDetail;
import com.ai.model.edu.cms.info.dto.InfoDetailDTO;
import com.ai.model.edu.cms.info.vo.InfoDetailVO;
import com.ai.service.cms.info.InfoDetailService;
import com.ai.service.user.PicRoomService;

/**
 * Class Name : InfoDetailServiceImpl<br>
 * 
 * Description : 这里记述class说明<br>
 * 
 * @author liuhb
 * @version $Revision$
 * @see
 *
 */
@Service("infoDetailService")
public class InfoDetailServiceImpl implements InfoDetailService {

	@Resource
	private InfoDetailManager infoDetailManager;
	@Resource
	private PicRoomService picRoomService;
	@Resource
	private BackendOperatorManager backendOperatorManager;

	@Override
	public List<InfoDetailVO> findInfoDetailByDto(InfoDetailDTO dto) {
		return infoDetailManager.findInfoDetailByDto(dto);
	}

	@Override
	public Pagination<InfoDetailVO> findInfoDetailPageByDto(InfoDetailDTO dto,
			boolean needCount) {
		AlikAssert.isNotNull(dto, "参数错误");
		// 查询分页
		Pagination<InfoDetailVO> ipg = new Pagination<InfoDetailVO>();
		List<InfoDetailVO> dto2 = infoDetailManager
				.findInfoDetailPageByDto(dto);
		if (CollectionUtils.isNotEmpty(dto2)) {
			List<Long> idList = CommonUtil.getValueList(dto2, "createBy");
			List<BackendOperatorVO> operators = backendOperatorManager
					.findBackendOperatorListByIdList(idList);
			Map<Long, String> userMap = CommonUtil.listforMap(operators, "id",
					"realName");
			for (InfoDetailVO infoDetailVO : dto2) {
				infoDetailVO.setCreateByName(userMap.get(infoDetailVO
						.getCreateBy()));
				Long locThemeId = infoDetailVO.getLocThemeId();
				if (locThemeId != null) {					
					infoDetailVO.setLocThemeName(WseSectionConstants.sectionMap
							.get(locThemeId.toString()));				
				}
			}
			ipg.setRows(dto2);
			if (needCount) {
				Long total = infoDetailManager
						.countFindInfoDetailPageByDto(dto);
				ipg.setTotal(total.intValue());
			}
		} else {
			ipg.setRows(dto2);
			ipg.setTotal(0);
		}
		return ipg;
	}

	@Override
	public Result deleteArticle(Long id, Long operator) {
		Result result = new Result(true, "");
		Long r = infoDetailManager.deleteInfoDetail(id, operator);
		if (r == 0) {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public Long insertInfoDetail(InfoDetail record) {
		Long res = null;
		MultipartFile uploadFile = record.getUploadFile();
		if (uploadFile != null && !uploadFile.isEmpty()) {
			Result result = picRoomService
					.uploadIdPhoto(uploadFile,
							"^.*?\\.(jpg|jpeg|png|gif|bmp)$", 2 * (2 << 19),
							true, null);
			String url = result.getAttribute("url").toString();
			if (StringUtils.isNotBlank(url)) {
				record.setFileUrl(url);
			}
		} else if (StringUtils.isNotBlank(record.getOldFile())) {
			record.setFileUrl(record.getOldFile());
		} else {
			record.setFileUrl(StringUtils.EMPTY);
		}
		String keyWord = record.getKeyWord();
		if (StringUtils.isNotBlank(keyWord)) {
			String str = keyWord;
			str = StringUtils.replaceChars(str, '，', ' ');
			str = StringUtils.replaceChars(str, ',', ' ');
			String[] separator = StringUtils.splitByWholeSeparator(str, null);
			StringBuilder sb = new StringBuilder();
			for (String string : separator) {
				if (sb.length() > 0) {
					sb.append(',');
				}
				sb.append(string);
			}
			record.setKeyWord(sb.toString());
		}

		if (record.getId() == null) {
			res = infoDetailManager.insertInfoDetail(record);
			res = record.getId();
		} else {
			record.setUpdateBy(record.getCreateBy());
			record.setCreateBy(null);
			record.setInfoStatus(Byte.valueOf("0"));
			res = infoDetailManager.updateInfoDetail(record);
			res = record.getId();
		}
		return res;
	}

	@Override
	public InfoDetailVO selectInfoDetailById(Long id) {
		return infoDetailManager.selectInfoDetailById(id);
	}

	@Override
	public List<InfoDetailVO> betchFindInfoDetailByIds(List<Long> idList) {
		List<InfoDetailVO> detailList = infoDetailManager
				.betchFindInfoDetailByIds(idList);
		List<InfoDetailVO> resultList = new ArrayList<InfoDetailVO>();
		for (InfoDetailVO detail : detailList) {
			if ("3".equals(detail.getInfoStatus().toString())) {// 已发布
				resultList.add(detail);
			}
		}
		return resultList;
	}

	/**
	 * Method Name : findInfoDetailByIds<br>
	 * 
	 * Description : 根据ids查询未审核的资讯信息<br>
	 * 
	 * @param idList
	 * @return
	 * @since
	 * 
	 */
	@Override
	public List<InfoDetailVO> findInfoDetailByIds(List<Long> idList) {
		List<InfoDetailVO> detailList = infoDetailManager
				.betchFindInfoDetailByIds(idList);
		List<InfoDetailVO> resultList = new ArrayList<InfoDetailVO>();
		for (InfoDetailVO detail : detailList) {
			if ("0".equals(detail.getInfoStatus().toString())) {// 已发布
				resultList.add(detail);
			}
		}
		return resultList;
	}

	/**
	 * 更新资讯记录
	 */
	public Long updateInfoDetail(InfoDetail record) {
		return infoDetailManager.updateInfoDetail(record);
	}

	@Override
	public InfoDetailVO findInfoDetailById(Long id) {
		return infoDetailManager.findInfoDetailById(id);
	}
	
	public Pagination<InfoDetailVO> findInfoDetailPageHyByDto(InfoDetailDTO dto,
			boolean needCount) {
		AlikAssert.isNotNull(dto, "参数错误");
		// 查询分页
		Pagination<InfoDetailVO> ipg = new Pagination<InfoDetailVO>();
		List<InfoDetailVO> dto2 = infoDetailManager
				.findInfoDetailPageByDto(dto);
		if (CollectionUtils.isNotEmpty(dto2)) {
			List<Long> idList = CommonUtil.getValueList(dto2, "createBy");
			List<BackendOperatorVO> operators = backendOperatorManager
					.findBackendOperatorListByIdList(idList);
			Map<Long, String> userMap = CommonUtil.listforMap(operators, "id",
					"realName");
			for (InfoDetailVO infoDetailVO : dto2) {
				infoDetailVO.setCreateByName(userMap.get(infoDetailVO
						.getCreateBy()));
				Long locThemeId = infoDetailVO.getLocThemeId();
				if (locThemeId != null) {
					infoDetailVO.setLocThemeName(WseSectionConstantsHy.sectionMap
							.get(locThemeId.toString()));				
				}
			}
			ipg.setRows(dto2);
			if (needCount) {
				Long total = infoDetailManager
						.countFindInfoDetailPageByDto(dto);
				ipg.setTotal(total.intValue());
			}
		} else {
			ipg.setRows(dto2);
			ipg.setTotal(0);
		}
		return ipg;
	}

}
